<?php
class ServicecardModel extends Model {
	protected $_validate = array(
	    array('title','require','请填写标题！'),
    );
	
	//送卡券
	public function sendCard($id, $member_id, $sale_id = 0, $order_id = 0){
		$card = $this->where(array('site_id' => __SITEID__, 'id' => $id)) -> find();
		
		if(!$card){return false;}
		
		$cdb  = M('Serviceclog');
		for($i = 0; $i < $card['num']; $i++){
			$data['member_id'] = $member_id;
			$data['servicecard_id'] = $id;
			$data['status'] = 0;
			$data['title'] = $card['title'];
			$data['dateline'] = time();
			$data['site_id'] = __SITEID__;
			$data['sale_id'] = $sale_id;
			$data['order_id'] = $order_id;
			$cdb -> add($data);
		}
	}
	
	
	public function _list($view,$map,$sort='id',$pagesize=20){
		//计算总页数
		$db = M('Serviceclog');
		$count=$db->where($map) -> group('servicecard_id') -> Field('count(*) as tp') -> find();
		//实例化分页类
		import ( 'ORG.Util.Page' );
		$p=new Page($count['tp'],$pagesize);
		$list = $db->where($map)->order($sort)->limit($p->firstRow.', 
'.$p->listRows)->Field("*, count(*) as num") ->select();
        $pages=$p->show();
		$view->assign('list',$list);
		$view->assign('pages',$pages);
	}
}